<template>
  <el-card class="community-card" :body-style="{ padding: '0px' }" shadow="hover">
    <el-image
      :src="data.cover || defaultCover"
      class="card-image"
      fit="cover"
    >
      <template #placeholder>
        <div class="image-slot">
          <el-icon class="is-loading">
            <Loading />
          </el-icon>
        </div>
      </template>
      <template #error>
        <div class="image-slot">
          <el-icon><Picture /></el-icon>
        </div>
      </template>
    </el-image>
    <div class="card-content">
      <div class="card-title">{{ data.title }}</div>
      <div class="card-description">{{ data.desc || data.description || '' }}</div>
      <div class="card-footer">
        <div class="card-author">
          <el-avatar :size="20" :src="data.author?.avatar" />
          <span class="author-name">{{ data.author?.nickname || '匿名用户' }}</span>
        </div>
        <div class="card-meta">
          <span class="meta-item">
            <el-icon><View /></el-icon>
            {{ data.viewNum || data.views || 0 }}
          </span>
          <span class="meta-item">
            <el-icon><Star /></el-icon>
            {{ data.likeNum || data.likes || 0 }}
          </span>
          <span class="meta-item">
            <el-icon><ChatDotRound /></el-icon>
            {{ data.commentNum || data.comments || 0 }}
          </span>
        </div>
      </div>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import type { CommunityItem } from '@/types/web/community'
import { Picture, Loading, View, Star, ChatDotRound } from '@element-plus/icons-vue'

interface Props {
  data: CommunityItem
}

const props = defineProps<Props>()

const defaultCover = 'https://via.placeholder.com/300x200?text=Community+Post'
</script>

<script lang="ts">
export default {
  name: 'CommunityCard'
}
</script>

<style scoped lang="scss">
.community-card {
  width: 300px;
  margin: 10px;
  cursor: pointer;
  transition: all 0.3s;

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  }

  .card-image {
    width: 100%;
    height: 200px;
    display: block;
  }

  .image-slot {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background: var(--el-fill-color-light);
    color: var(--el-text-color-secondary);
    font-size: 30px;
  }

  .card-content {
    padding: 15px;
  }

  .card-title {
    font-size: 16px;
    font-weight: bold;
    color: #303133;
    margin-bottom: 8px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .card-description {
    font-size: 14px;
    color: #606266;
    line-height: 1.5;
    height: 42px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    margin-bottom: 12px;
  }

  .card-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-top: 8px;
    border-top: 1px solid #eee;
  }

  .card-author {
    display: flex;
    align-items: center;
    gap: 8px;

    .author-name {
      font-size: 12px;
      color: #909399;
    }
  }

  .card-meta {
    display: flex;
    gap: 12px;

    .meta-item {
      display: flex;
      align-items: center;
      gap: 4px;
      font-size: 12px;
      color: #909399;

      .el-icon {
        font-size: 14px;
      }
    }
  }
}
</style>

